package com.falcon.scanner.impl;

import com.falcon.parser.ControllerMetaParser;
import com.falcon.scanner.IControllerScanner;
import com.falcon.domain.vo.ControllerMeta;
import com.falcon.mvc.base.IBaseController;
import com.swan.core.components.ApplicationContextHolder;
import com.swan.core.utils.ClassUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zongf
 * @date 2024-03-15
 **/
@Slf4j
@Service
public class ControllerScanner implements IControllerScanner {

    @Autowired
    private ApplicationContextHolder applicationContextHolder;

    @Autowired
    private ControllerMetaParser controllerMetaParser;

    @Override
    public List<ControllerMeta> doScan(boolean parseMethod, boolean parseMethodParam, boolean parseMethodReturn) {
        List<ControllerMeta> list = new ArrayList<>();

        for (String beanName : applicationContextHolder.getBeanNames(IBaseController.class)) {
            BeanDefinition beanDefinition = applicationContextHolder.getBeanDefinition(beanName);
            Class<? extends IBaseController> clz = ClassUtil.forName(beanDefinition.getBeanClassName(), applicationContextHolder.getClass().getClassLoader());

            ControllerMeta controllerMeta = this.controllerMetaParser.parseController(clz, parseMethod, parseMethodParam, parseMethodReturn);
            list.add(controllerMeta);
        }
        return list;
    }

}
